<template>
  <!-- 代理升降级配置 -->
  <el-dialog :visible.sync="isShow" v-if="isShow" title="代理升降级配置" width="55%" :before-close="handleClose" top="100px" :close-on-click-modal="false">
    <div class="border-box">
      <el-form ref="formParams" :model="formParams" :rules="giftForm" label-position="right" label-width="0">
        <div class="flex item-box" v-for="(item,index) in itemData" :key="index">
          <div class="title width25">
            {{ item.label }}<span :class="item.pass?'mustPass':'noMustPass'">*</span>
          </div>
          <div class="content width80">
            <!-- 代理名称 -->
            <el-form-item class="item-clear" v-show="item.key=='a_name'" label="" prop="a_name">
              <el-input v-model="formParams.a_name" clearable placeholder="请输入代理名称"/>
            </el-form-item>
            <!-- 代理等级 -->
            <el-form-item class="item-clear" v-show="item.key=='a_level'" label="" prop="a_level">
              <el-input-number :min="0" :precision="0" v-model="formParams.a_level" controls-position="right" clearable placeholder="请输入代理等级"/>
            </el-form-item>
            <!-- 升级经验 -->
            <el-form-item class="item-clear" v-show="item.key=='a_exp'" label="" prop="a_exp">
              <el-input-number :min="0" :precision="0" v-model="formParams.a_exp" controls-position="right" clearable placeholder="请输入升级经验"/>
            </el-form-item>
            <!-- 邀请每个人获得的卢比 -->
            <el-form-item class="item-clear" v-show="item.key=='a_money'" label="" prop="a_money">
              <el-input-number :min="0" :precision="0" v-model="formParams.a_money" controls-position="right" clearable placeholder="请输入邀请每个人获得的卢比"/>
            </el-form-item>
            <!-- 卢比和经验比 -->
            <el-form-item class="item-clear" v-show="item.key=='a_exp_proportion'" label="" prop="a_exp_proportion">
              <el-input-number :min="0" :max="10000" :precision="0" v-model="formParams.a_exp_proportion" controls-position="right" clearable placeholder="请输入卢比和经验比例"/>
            </el-form-item>
            <!-- 每日邀请人数达标x数量，不足人数降低X经验 -->
            <div class="item-clear" v-show="item.key=='a_person_target'" style="height:auto;">
              <div class="flexWrap">
                每日邀请人数达标
                <el-form-item label="" prop="person_target_num">
                  <el-input-number class="width150" :min="0" :max="999999999999" :precision="0" v-model="formParams.person_target_num" :controls="false" clearable placeholder="请输入数量"/>
                </el-form-item>数,
                不足人数降低
                <el-form-item label="" prop="person_target_exp">
                  <el-input-number class="width150" :min="0" :max="10000" :precision="0" v-model="formParams.person_target_exp" :controls="false" clearable placeholder="请输入经验"/>
                </el-form-item>经验
              </div>
            </div>
            <!-- x天直属充值不达标x额度，降x经验 -->
            <div class="item-clear" v-show="item.key=='a_recharge_target'" style="height:auto;">
              <div class="flexWrap">
                <el-form-item label="" prop="recharge_target_day">
                  <el-input-number class="width120" :min="0" :max="999999999999" :precision="0" v-model="formParams.recharge_target_day" :controls="false" clearable placeholder="请输入天数"/>
                </el-form-item>天,直属充值不达标
                <el-form-item label="" prop="recharge_target_num">
                  <el-input-number class="width180" :min="0" :max="10000" :precision="0" v-model="formParams.recharge_target_num" :controls="false" clearable placeholder="请输入额度"/>
                </el-form-item>额度,降低
                <el-form-item label="" prop="recharge_target_exp">
                  <el-input-number class="width150" :min="0" :max="10000" :precision="0" v-model="formParams.recharge_target_exp" :controls="false" clearable placeholder="请输入经验"/>
                </el-form-item>经验
              </div>
            </div>
            <!-- 自己x天不上线，降x经验 -->
            <div class="item-clear" v-show="item.key=='a_online_target'" style="height:auto;">
              <div class="flexWrap">
                每日邀请人数达标
                <el-form-item label="" prop="online_target_day">
                  <el-input-number class="width150" :min="0" :max="999999999999" :precision="0" v-model="formParams.online_target_day" :controls="false" clearable placeholder="请输入天数"/>
                </el-form-item>数,
                不足人数降低
                <el-form-item label="" prop="online_target_exp">
                  <el-input-number class="width150" :min="0" :max="10000" :precision="0" v-model="formParams.online_target_exp" :controls="false" clearable placeholder="请输入经验"/>
                </el-form-item>经验
              </div>
            </div>
            <!-- X天自己充值不达标X额度，降X经验 -->
            <div class="item-clear" v-show="item.key=='a_rechargeself_target'" style="height:auto;">
              <div class="flexWrap">
                <el-form-item label="" prop="rechargeself_target_day">
                  <el-input-number class="width120" :min="0" :max="999999999999" :precision="0" v-model="formParams.rechargeself_target_day" :controls="false" clearable placeholder="请输入天数"/>
                </el-form-item>天,自己充值不达标
                <el-form-item label="" prop="rechargeself_target_num">
                  <el-input-number class="width180" :min="0" :max="10000" :precision="0" v-model="formParams.rechargeself_target_num" :controls="false" clearable placeholder="请输入额度"/>
                </el-form-item>额度,降低
                <el-form-item label="" prop="rechargeself_target_exp">
                  <el-input-number class="width150" :min="0" :max="10000" :precision="0" v-model="formParams.rechargeself_target_exp" :controls="false" clearable placeholder="请输入经验"/>
                </el-form-item>经验
              </div>
            </div>
          </div>
        </div>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="medium" @click="handleClose">取 消</el-button>
      <el-button size="medium" type="primary" @click="handleConfirm('formParams')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  addProxy,
  editProxy
} from '@/api/game'
import {
  GAME_TYPE
} from '@/utils/enum'
export default {
  components: {},
  props: {
    show: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default: function () {
        return {}
      }
    },
  },
  data () {
    return {
      GAME_TYPE,
      isShow: false,
      itemData:[
        { label: '代理名称', key: 'a_name' ,pass:true},
        { label: '代理等级', key: 'a_level' ,pass:true},
        { label: '升级经验', key: 'a_exp' ,pass:true},
        { label: '邀请每个人获得的卢比', key: 'a_money' },
        { label: '卢比和经验比例', key: 'a_exp_proportion'},
        { label: '每日邀请', key: 'a_person_target'},
        { label: '直属充值', key: 'a_recharge_target'},
        { label: '不上线惩罚', key: 'a_online_target'},
        { label: '充值不达标', key: 'a_rechargeself_target'},
      ],
      formParams: {
        a_name:'',
        a_level: 0,
        a_exp: 0,
        a_money: 0,
        a_exp_proportion: 0,

        person_target_num: 0,
        person_target_exp: 0,
        a_person_target: '',

        recharge_target_day: 0,
        recharge_target_num: 0,
        recharge_target_exp: 0,
        a_recharge_target: '',
        
        online_target_day: 0,
        online_target_exp: 0,
        a_online_target: '',

        rechargeself_target_day: 0,
        rechargeself_target_num: 0,
        rechargeself_target_exp: 0,
        a_rechargeself_target: '',
      },
      giftForm: {
        a_name: [{ required: true, trigger: 'blur', message: '请输入代理名称', }],
        a_level: [{ required: true, trigger: 'blur', message: '请输入代理等级', }],
        a_exp: [{ required: true, trigger: 'blur', message: '请输入升级经验', }],
      },
      roleDataList: [],
    }
  },
  computed: {},
  watch: {
    show: function () {
      let that = this;
      that.isShow = that.show
      if (that.data) {
        that.data.person_target_num=0
        that.data.person_target_exp=0
        that.data.recharge_target_day=0
        that.data.recharge_target_num=0
        that.data.recharge_target_exp=0
        that.data.online_target_day=0
        that.data.online_target_exp=0
        that.data.rechargeself_target_day=0
        that.data.rechargeself_target_num=0
        that.data.rechargeself_target_exp=0
        that.formParams = JSON.parse(JSON.stringify(that.data))
        let params = that.formParams;
        // 每日邀请
        if(params.a_person_target){
          params.person_target_num=params.person_target[0]
          params.person_target_exp=params.person_target[1]
        }
        // 直属充值
        if(params.a_recharge_target){
          params.recharge_target_day=params.recharge_target[0]
          params.recharge_target_num=that.$public.mathNumberDivideWan(params.recharge_target[1])
          params.recharge_target_exp=params.recharge_target[2]
        }
        // 每日邀请
        if(params.a_online_target){
          params.online_target_day=params.online_target[0]
          params.online_target_exp=params.online_target[1]
        }
        // 每日邀请
        if(params.a_rechargeself_target){
          params.recharge_target_day=params.rechargeself_target[0]
          params.rechargeself_target_num=that.$public.mathNumberDivideWan(params.rechargeself_target[1])
          params.rechargeself_target_exp=params.rechargeself_target[2]
        }
      } else {
        that.formParams = {
          a_name:'',
          a_level: 0,
          a_exp: 0,
          a_money: 0,
          a_exp_proportion: 0,
          
          person_target_num: 0,
          person_target_exp: 0,
          a_person_target: '',

          recharge_target_day: 0,
          recharge_target_num: 0,
          recharge_target_exp: 0,
          a_recharge_target: '',
          
          online_target_day: 0,
          online_target_exp: 0,
          a_online_target: '',

          rechargeself_target_day: 0,
          rechargeself_target_num: 0,
          rechargeself_target_exp: 0,
          a_rechargeself_target: '',
        }
      }
    },
  },
  mounted () {},
  methods: {
    // 确认
    handleConfirm (formvalue) {
      let that = this, params = JSON.parse(JSON.stringify(that.formParams));
      // 每日邀请
      if(!params.person_target_num && !params.person_target_exp){
        params.a_person_target=''
      }else{
        params.a_person_target=params.person_target_num+','+params.person_target_exp
      }
      // 直属充值
      if(!params.recharge_target_day && !params.recharge_target_num && !params.recharge_target_exp){
        params.a_recharge_target=''
      }else{
        params.a_recharge_target=params.recharge_target_day+','+params.recharge_target_num*10000+','+params.recharge_target_exp
      }
      // 每日邀请
      if(!params.online_target_day && !params.online_target_exp){
        params.a_online_target=''
      }else{
        params.a_online_target=params.online_target_day+','+params.online_target_exp
      }
      // 每日邀请
      if(!params.rechargeself_target_day && !params.rechargeself_target_num && !params.rechargeself_target_exp){
        params.a_rechargeself_target=''
      }else{
        params.a_rechargeself_target=params.rechargeself_target_day+','+params.rechargeself_target_num*10000+','+params.rechargeself_target_exp
      }
      params.a_money=that.$public.mathNumberMultiply(params.a_money,10000)
      that.$refs[formvalue].validate((valid) => {
        if (valid) {
          // 新增
          if (!params.id) {
            addProxy(params).then(function (res) {
              that.$message.success(res.msg)
              that.$emit('onHideDetails', true)
            }).catch((error) => {
              that.$message.error(error.msg)
            })
          } else {
            editProxy(params).then(function (res) {
              that.$message.success(res.msg)
              that.$emit('onHideDetails', true)
            }).catch((error) => {
              that.$message.error(error.msg)
            })
          }
        } else {
          return false;
        }
      });
    },
    // 取消
    handleClose () {
      this.$emit('onHideDetails', false)
    },
  }
}
</script>

<style scoped lang="scss">
.el-form-item{
  margin-bottom: 0;
}
.item-clear {
  font-size: 17px;
  font-weight: 600;
}
</style>
